<?php

require_once dirname(__FILE__) . '/../bootstrap.unit.php';

class ForageTest extends ForageUnitTestCase
{
  private $forage;

  public function setUp()
  {
    $this->forage = Forage::create('test:test');
  }

  public function tearDown()
  {
    unset($this->forage);
  }

  public function testCreatingQueryObject()
  {
    $query = $this->forage->getQuery();
    $this->assertInstanceOf($query, 'ForageQuery');
  }

  public function testCreatingQueryWithQueryString()
  {
    $query = $this->forage->getQuery('forage');
    $this->assertEquals('forage', $query->query_string);

    $query = $this->forage->getQuery('search');
    $this->assertEquals('search', $query->query_string);
  }

  public function testCustomQueryObjectCanBeReturned()
  {
    $forage = Forage::create('solr:127.0.0.1:8080');
    $query  = $forage->getQuery();
    $this->assertInstanceOf($query, 'ForageSolrQuery');
  }

  public function testSearchCreatesSimpleQueryWhenGivenAString()
  {
    $query = $this->forage->search('foo');
    $this->assertInstanceOf($query, 'ForageQuery');
  }

  public function testSettingEngine()
  {
    $default_engine = $this->forage->engine;
    $new_engine     = ForageEngine::create('test:test2', array());
    $this->forage->setEngine($new_engine);
    $this->assertSame($new_engine, $this->forage->engine);
  }

  /**
   * @expectedException BadMethodCallException
   */
  public function testFailureOfBadMethodCall()
  {
    $this->forage->badMethodWhichIKnowDoesNotExist();
  }

  /**
   * @expectedException ForageException
   */
  public function testGettingNoProfiler()
  {
    $this->forage->profiler;
  }

  public function testStartingProfiler()
  {
    $this->forage->startProfiler();
    $this->assertInstanceOf($this->forage->profiler, 'ForageProfiler');
  }

  public function testStoppingProfiler()
  {
    $this->forage->startProfiler();
    $this->forage->stopProfiler();
    try {
      $this->forage->profiler;
      $this->fail();
    } catch (ForageException $e) { }
  }

  public function testRunningProfiler()
  {
    $this->forage->startProfiler();
    $doc = new ForageDocument();
    $this->forage->add($doc);
    $this->forage->add($doc);
    $hits = $this->forage->profiler->hits('add');
    $this->assertEquals($hits, 2);
  }

  public function testProfilingSpecificMethods()
  {
    $this->forage->startProfiler();
    $this->forage->search('');
    $this->assertEquals(0, $this->forage->profiler->hits('search'));
    $this->forage->setProfileMethods(array('search'));
    $this->forage->search('');
    $this->assertEquals(1, $this->forage->profiler->hits('search'));
  }

  public function testSettingProfilerMethodsFromConstruction()
  {
    $forage = Forage::create('test:test', array('profile_methods'=>array('search')));
    $forage->startProfiler();
    $forage->search('');
    $this->assertEquals(1, $forage->profiler->hits('search'));
  }

  /**
   * @expectedException InvalidArgumentException
   */
  public function testFailureOnSettingBadProfilerMethodArray()
  {
    $this->forage->setProfileMethods('foo');
  }
}
